<script setup lang="ts">
import { getMemberAddressAPI } from "@/api/address";
import { onMounted, onUnmounted, ref, watch } from "vue";

// 选中的地址 id
const selectedId = ref<number | null>(1);
// 点击关闭按钮
const close = () => {
  // 通知父组件关闭弹窗
  emit("close");
};
// 选中地址
const selectAddress = (addr: any) => {
  selectedId.value = addr.id;
  // 把选中的地址返回给父组件
  emit("select", addr);
  // 关闭弹窗
  emit("close");
};
const addressList = ref<any>([]);
const getMemberAddressData = async () => {
  const res: any = await getMemberAddressAPI();
  addressList.value = res.data;
  console.log("addressList", addressList.value);
};
onMounted(() => {
  getMemberAddressData();
  // 监听地址更新事件
  uni.$on("address-updated", () => {
    console.log("收到地址更新事件，刷新列表");
    getMemberAddressData();
  });
});
// emit
const emit = defineEmits<{
  (e: "close"): void;
  (e: "select", addr: any): void;
}>();
onUnmounted(() => {
  uni.$off("address-updated");
});
</script>

<template>
  <view class="address-panel">
    <!-- 关闭按钮 -->
    <text class="close icon-close" @tap="close"></text>

    <!-- 标题 -->
    <view class="title">配送至</view>

    <!-- 内容 -->
    <view class="content">
      <view v-for="addr in addressList" :key="addr.id" class="item" @tap="selectAddress(addr)">
        <view class="user">{{ addr.receiver }} {{ addr.contact }}</view>
        <view class="address">{{ addr.fullLocation }}{{ addr.address }}</view>
        <text class="icon" :class="selectedId === addr.id ? 'icon-checked' : 'icon-ring'"></text>
      </view>
    </view>

    <!-- 底部 -->
    <view class="footer">
      <view class="button primary">
        <navigator hover-class="none" :url="`/pagesMember/address-form/address-form`">
          新建地址
        </navigator>
      </view>
    </view>
  </view>
</template>

<style lang="scss">
.address-panel {
  padding: 0 30rpx;
  border-radius: 10rpx 10rpx 0 0;
  position: relative;
  background-color: #fff;
}

.title {
  line-height: 1;
  padding: 40rpx 0;
  text-align: center;
  font-size: 32rpx;
  font-weight: normal;
  border-bottom: 1rpx solid #ddd;
  color: #444;
}

.close {
  position: absolute;
  right: 24rpx;
  top: 24rpx;
}

.content {
  min-height: 300rpx;
  max-height: 540rpx;
  overflow: auto;
  padding: 20rpx;

  .item {
    padding: 30rpx 50rpx 30rpx 60rpx;
    background-size: 40rpx;
    background-repeat: no-repeat;
    background-position: 0 center;
    background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png);
    position: relative;
  }

  .icon {
    color: #999;
    font-size: 40rpx;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 0;
  }

  .icon-checked {
    color: #27ba9b;
  }

  .icon-ring {
    color: #444;
  }

  .user {
    font-size: 28rpx;
    color: #444;
    font-weight: 500;
  }

  .address {
    font-size: 26rpx;
    color: #666;
  }
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0 40rpx;
  font-size: 28rpx;
  color: #444;

  .button {
    flex: 1;
    height: 72rpx;
    text-align: center;
    line-height: 72rpx;
    margin: 0 20rpx;
    color: #fff;
    border-radius: 72rpx;
  }

  .primary {
    color: #fff;
    background-color: #27ba9b;
  }

  .secondary {
    background-color: #ffa868;
  }
}
</style>
